﻿using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace YinLongTool.Serialize
{
	// Token: 0x02000013 RID: 19
	public class XMLSerializer
	{
		// Token: 0x0600004D RID: 77 RVA: 0x000034EC File Offset: 0x000016EC
		public static void XmlSerialize(object type, string fileName)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(type.GetType());
			Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read);
			xmlSerializer.Serialize(stream, type);
			stream.Close();
		}

		// Token: 0x0600004E RID: 78 RVA: 0x00003520 File Offset: 0x00001720
		public static string XmlSerialize(object type)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(type.GetType());
			StringBuilder stringBuilder = new StringBuilder();
			TextWriter textWriter = new StringWriter(stringBuilder);
			xmlSerializer.Serialize(textWriter, type);
			textWriter.Close();
			return stringBuilder.ToString();
		}

		// Token: 0x0600004F RID: 79 RVA: 0x00003564 File Offset: 0x00001764
		public static T XmlDeserialize<T>(string fileName) where T : class, new()
		{
			try
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
				T t = xmlSerializer.Deserialize(stream) as T;
				stream.Close();
				if (t == null)
				{
					t = Activator.CreateInstance<T>();
				}
				return t;
			}
			catch
			{
			}
			return Activator.CreateInstance<T>();
		}

		// Token: 0x06000050 RID: 80 RVA: 0x000035E4 File Offset: 0x000017E4
		public static T DeserializeFromXmlString<T>(string xmlstring) where T : class, new()
		{
			try
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				TextReader textReader = new StringReader(xmlstring);
				T t = xmlSerializer.Deserialize(textReader) as T;
				textReader.Close();
				if (t == null)
				{
					t = Activator.CreateInstance<T>();
				}
				return t;
			}
			catch
			{
			}
			return Activator.CreateInstance<T>();
		}
	}
}
